import jsonp from './jsonp'
import axios from 'axios'
export default class Song {
  constructor (id, mid, singer, name, albm, duration, image) {
    this.id = id
    this.mid = mid
    this.name = name
    this.singer = singer
    this.albm = albm
    this.duration = duration
    this.image = image
  }
}

export function createSong (musicData) {
  // console.log(musicData)
  return new Song(
    musicData.songid,
    musicData.songmid,
    filterSinger(musicData.singer),
    musicData.songname,
    musicData.albumname,
    musicData.interval,
    `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`
  )
}

function filterSinger (singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  return ret.join('/')
}
var t = (new Date()).getUTCMilliseconds()
var guid = Math.round(2147483647 * Math.random()) * t % 1e10
export function getSongVkey(songmid) {
  const url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'
  const data = Object.assign({}, {
    callback: 'musicJsonCallback',
    loginUin: 3051522991,
    format: 'jsonp',
    platform: 'yqq',
    needNewCode: 0,
    cid: 205361747,
    uin: 0,
    guid: guid,
    // guid: 0,
    songmid: songmid,
    filename: `C400${songmid}.m4a`
  })
  return jsonp(url, data)
}

export function getSongVkey1(songmid) {
  const url = '/getPurlUrl'
  const data = Object.assign({}, {
    // songmid: songmid,
    // notice: 0,
    // platform: 'yqq.json',
    // needNewCode: 0,
    // format: 'json',
    // data: {"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"9244517832","songmid":["001Qu4I30eVFYb"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}
  })
  return axios.post(url, {
    data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}
var vkey = ''
var filename = ''
export function getVkey(songmid) {
  let ss = getSongVkey(songmid).then((res) => {
    let data = res.data
    // console.log(songmid)
    vkey = data.items[0].vkey
    filename = data.items[0].filename
    return {vkey, filename}
  })
  console.log(ss)
  // setTimeout(() => {
  //   return `http://aqqmusic.tc.qq.com/amobile.music.tc.qq.com/${filename}?guid=${guid}&vkey=${vkey}&uin=0&fromtag=38`
  if (filename) {
    return `http://aqqmusic.tc.qq.com/amobile.music.tc.qq.com/${filename}?guid=${guid}&vkey=${vkey}&uin=0&fromtag=38`
  } else {
    return '123'
  }
  // }, 1000)
}

export function VkeyAxios(songmid) {
  // const url = '/vkey'
  const url1 = '/getPurlUrl'
  // var t = (new Date()).getUTCMilliseconds()
  const data1 = Object.assign({}, {
    comm: {
      format: 'json',
      g_tk: 5381,
      inCharset: 'utf-8',
      needNewCode: 1,
      notice: 0,
      outCharset: 'utf-8',
      platform: 'h5',
      uin: 0},
    req0: {
      method: 'CgiGetVkey',
      module: 'vkey.GetVkeyServer',
      param: {
        guid: '123567029',
        loginflag: 0,
        platform: '23',
        uin: 0,
        songmid: ['003oF0Pc0ahSx4', '000rBgbe4K0vuz', '003upNAA061t8N', '0025Jphd3gmIrD', '001NiBuI1Jg0gt',
          '0012HIms0X4Yoc', '002I3Nwa4f9xqA', '004TXEXY2G2c7C', '002E8ebM3TFWmA', '002Ce1kE4crzRK',
          '002QoT9n3hcEIr', '001PLl3C4gPSCI'],
        songtype: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      }
    }
  })
  // const data = Object.assign({}, {
  //   callback: 'musicJsonCallback',
  //   loginUin: 3051522991,
  //   format: 'json',
  //   platform: 'yqq',
  //   needNewCode: 0,
  //   cid: 205361747,
  //   uin: 0,
  //   guid: Math.round(2147483647 * Math.random()) * t % 1e10,
  //   songmid: songmid,
  //   // guid: 0,
  //   filename: `C400${songmid}.m4a`
  // })
  return axios.post(url1, data1).then((res) => {
    return Promise.resolve(res.data)
  })
}
